*首先请安装以下命令
ssc install _gwtmean, replace  // 保证wtmean命令正常运行
ssc install palettes, replace  //保证colorpalette命令正常运行
ssc install colrspace, replace

*导入数据
use "bll_processed_dataset.dta", clear

*设置控制变量的全局变量
global control_list unemp_rate median_income  percent_non_white lead_emitted ap

*设置colorpalette
colorpalette hsv, dark n(3) nograph
return list

*******************************
**DID，用来估计前两年平均效应**
*******************************

*DID回归，被解释变量ihs_bll，交叉项i.pre_2007##i.race
reghdfe ihs_bll        ///
	i.pre_2007##i.race ///
	$control_list      ///
	[aw=weight]        ///
	, absorb(county_id i.StateFIPS##i.year) ///
      vce(cluster county_id)

*回归系数与p统计量
local nascar_estimate = round(_b[1.pre_2007#2.race],.01)
local border_estimate = round(_b[1.pre_2007#1.race],.01)
local nascar_p = 2*ttail(e(df_r),_b[1.pre_2007#2.race]/_se[1.pre_2007#2.race])
local border_p = 2*ttail(e(df_r),_b[1.pre_2007#1.race]/_se[1.pre_2007#1.race])

*设置显著性水平星星个数
if `nascar_p' <= .01 {
	    local nascar_stars = "***"
	}
	else if `nascar_p' <= .05 {
	    local nascar_stars = "**"
	}
	else if `nascar_p' <= .1 {
	    local nascar_stars = "*"
	}
	else {
	    local nascar_stars = ""
	}

if `border_p' <= .01 {
	    local border_stars = "***"
	}
	else if `border_p' <= .05 {
	    local border_stars = "**"
	}
	else if `border_p' <= .1 {
	    local border_stars = "*"
	}
	else {
	    local border_stars = ""
	}
	di "`border_stars'"

*******************
**DID政策动态效应**
*******************

*DID政策动态效应回归，ib2007.year：因子变量，以2007年为基年
reghdfe ihs_bll ///
	ib2007.year##i.race ///
	$control_list ///
	[aw=weight]   ///
	, absorb(county_id i.StateFIPS##i.year) vce(cluster county_id)

*若存在order,b,high,low变量，则删除
capture drop order
capture drop border*
capture drop b high low

/*设定order,b,high,low变量，默认值为空
b指回归系数，high和low分别指置信区间上下界，order为年份绘图顺序
无border前缀为比赛县数据，有border前缀为邻县比赛的县数据*/
gen order = .
gen border_b =.
gen border_high =.
gen border_low =.
gen b =.
gen high =.
gen low =.
local i = 1

*对2005-2006年order,b,high,low变量赋值
forvalues yr = 2005(1)2006{
	replace order = `i' in `i'

	replace border_b    = _b[`yr'.year#1.race] in `i'
	replace border_high = _b[`yr'.year#1.race] + 1.96*_se[`yr'.year#1.race] in `i'
	replace border_low 	= _b[`yr'.year#1.race] - 1.96*_se[`yr'.year#1.race] in `i'

	replace b    = _b[`yr'.year#2.race] in `i'
	replace high = _b[`yr'.year#2.race] + 1.96*_se[`yr'.year#2.race] in `i'
	replace low  = _b[`yr'.year#2.race] - 1.96*_se[`yr'.year#2.race] in `i'

	local i = `i' + 1
}

*对2007年order,b,high,low变量赋值（此时`i'=3）
replace order = `i' in `i'

replace border_b    = 0  in `i'
replace border_high = 0  in `i'
replace border_low  = 0  in `i'

replace b    = 0  in `i'
replace high = 0  in `i'
replace low  = 0  in `i'
local i = `i' + 1

*对2008-2015年order,b,high,low变量赋值（此时`i'=4）
forvalues yr = 2008(1)2015{
	replace order = `i' in `i'

	replace border_b 	= _b[`yr'.year#1.race] in `i'
	replace border_high = _b[`yr'.year#1.race] + 1.96*_se[`yr'.year#1.race] in `i'
	replace border_low 	= _b[`yr'.year#1.race] - 1.96*_se[`yr'.year#1.race] in `i'

	replace b    = _b[`yr'.year#2.race] in `i'
	replace high = _b[`yr'.year#2.race] + 1.96*_se[`yr'.year#2.race] in `i'
	replace low  = _b[`yr'.year#2.race] - 1.96*_se[`yr'.year#2.race] in `i'

	local i = `i' + 1
}


**********************
**绘制上图（比赛县）**
**********************

*设置配色方案
colorpalette hsv, dark n(3) nograph
return list
*设置文字位置
local year_pointer_label = _b[2008.year#2.race] - 1.96*_se[2008.year#2.race]
local county_label = _b[2008.year#2.race] - 1.96*_se[2008.year#2.race] -.07
local year_pointer_end = `year_pointer_label' + .03
local pointer_lower = `nascar_estimate' + .02
local pointer_upper = `nascar_estimate' + .07
local text_location = `pointer_upper' + 0.025

*绘图
twoway rarea low high order if order<18, fcol(gs14) lcol(white) msize(3) /// 估计系数
	|| connected b order if order<18, lw(1.1) col(white) msize(7) msymbol(s) lp(solid) /// 强调
		text(`county_label' 1 "Race counties", place(e) si(7)) ///
	|| connected b order if order<18, lw(0.6) col("71 71 179") msize(5) msymbol(s) lp(solid) /// 连线
	|| scatteri 0 1 0 11, recast(line) lcol(gs8) lp(longdash) lwidth(0.5) /// y=0基准线
	|| pcarrowi `year_pointer_end' 3 -.03 3, mlcolor(black) mfcolor(black) lcolor(black) barbsize(1.5) /// 指向第一年使用无铅汽油的箭头
		text(`year_pointer_label' 2.8 "First majority unleaded year", place(e) si(large)) /// 添加文字"First majority unleaded year"
	|| scatteri `nascar_estimate' 1 `nascar_estimate' 2.55, recast(line) lcol(black) lp(---)  /// 2005-2006年的平均效应，数值由DID给出
	|| pcarrowi `pointer_upper' 2.2 `pointer_lower' 2.2, mlcolor(black) mfcolor(black) lcolor(black) barbsize(1.5) /// 指向平均效应的箭头
		text(`text_location' 1.8 "Average effect relative to 2007-2015: `nascar_estimate'{sup:`nascar_stars'}", place(e) si(large)) /// 添加文字"Average effect relative to 2007-2015:.16***"
		xlab(1 "2005" 2 "2006" 3 "2007" 4 "2008" 5 "2009" 6 "2010" 7 "2011" ///
			 8 "2012" 9 "2013" 10 "2014"  11 "2015" /// 横坐标设置
				, nogrid labsize(7) angle(0)) ///
		ylab(, nogrid labs(7)) ///
		legend(off) ///
		xtitle("", size(7))
// 文件保存
graph save bll.gph, replace

****************************
**绘制下图（邻县有比赛县）**
****************************

// 设置文字位置
local year_pointer_label = _b[2015.year#1.race] - 1.96*_se[2015.year#1.race]
local year_pointer_end = `year_pointer_label' + .03
local pointer_lower = `border_estimate' + .02
local pointer_upper = `border_estimate' + .07
local text_location = `pointer_upper' + 0.025

// 绘图
twoway rarea border_low border_high order if order<18, fcol(gs14) lcol(white) msize(3) /// 估计系数
	|| connected border_b order if order<18, lw(1.1) col(white) msize(7) msymbol(d) lp(solid) /// 强调
		text(`county_label' 1 "Border counties", place(e) si(7)) ///
	|| connected border_b order if order<18, lw(0.6) col("71 179 71") msize(5) msymbol(d) lp(solid) /// 连线
	|| scatteri 0 1 0 11, recast(line) lcol(gs8) lp(longdash) lwidth(0.5) /// y=0基准线
	|| scatteri `border_estimate' 1 `border_estimate' 2.55, recast(line) lcol(black) lp(---) /// 2005-2006年的平均效应，数值由DID给出
	|| pcarrowi `pointer_upper' 2.2 `pointer_lower' 2.2, mlcolor(black) mfcolor(black) lcolor(black) barbsize(1.5) /// 指向平均效应的箭头
	text(`text_location' 1.8 "Average effect relative to 2007-2015: `border_estimate'{sup:`border_stars'}", place(e) si(large)) /// 添加文字"Average effect relative to 2007-2015:.05"
		xlab(1 "2005" 2 "2006" 3 "2007" 4 "2008" 5 "2009" 6 "2010" 7 "2011" ///
			 8 "2012" 9 "2013" 10 "2014"  11 "2015" /// 横坐标设置
				, nogrid labsize(7) angle(0)) ///
		ylab(, nogrid labs(7)) ///
		legend(off) ///
		xtitle("", size(7))

graph save bll_border.gph, replace

**************
**上下图合并**
**************

graph combine     ///
	"bll"         /// 上图文件名（比赛县）
	"bll_border" /// 下图文件名（邻县比赛的县）
	, altshrink  ycommon col(1) ysize(4.5) ///
	subtitle("Effect of a race on asinh(EBLL)", size(4) pos(11))
graph export figure_5_bll_event_study.png, replace
graph export figure_5_bll_event_study.pdf, replace


